﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace 深浅复制
{
    class Program
    {
        static void Main(string[] args)
        {
            Room room1 = new Room(60);
            Film film1 = new Film("值类型初始000001");
            Cinema cinema1 = new Cinema(room1, film1);
            Console.WriteLine("拷贝之前，{0}----{1}", cinema1._film._name, cinema1._room._maxSeat);
            Cinema cinema2 = (Cinema)cinema1.Cloine2();
            Console.WriteLine("拷贝之后，{0}----{1}", cinema2._film._name, cinema2._room._maxSeat);
            Console.WriteLine("================");
            cinema1._film._name = "值类型修改000001";
            cinema1._room._maxSeat = 80;

            Console.WriteLine("修改之后的原数据cinema1{0}------{1}", cinema1._film._name, cinema1._room._maxSeat);
            Console.WriteLine("修改之后修改后的cinema2{0}------{1}", cinema2._film._name, cinema2._room._maxSeat);

            // 因为浅复制，复制的是引用，所以修改后就把原先的对象给修改了




        }
    }

    [Serializable]
    public class Room
    {
        public Room()
        {

        }
        public int _maxSeat;

        public Room(int maxSeat)
        {
            this._maxSeat = maxSeat;
        }
    }
    [Serializable]
    public struct Film
    {
        public string _name;

        public Film(string name)
        {
            this._name = name;
        }
    }
    [Serializable]
    public class Cinema
    {
        public Room _room;
        public Film _film;
       
        public Cinema(Room room, Film film)
        {
            this._room = room;
            this._film = film;
        }

        public object Clone()
        {
            return MemberwiseClone(); //对引用类型实施浅复制
        }

        public object Clone1()
        {
            Room room = new Room();
            room._maxSeat = this._room._maxSeat;//复制当前引用类型成员的值到新对象 
            Film film = this._film; //值类型直接赋值
            Cinema cinema = new Cinema(room, film);
            return cinema;

        }

        public object Cloine2()
        {
            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, this); //复制到流中
            ms.Position = 0;
            return (bf.Deserialize(ms));

        }


    }




}
